(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid,perid
	var domArr = []
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//批评 criticFrom|64||1^criticDate|2022-08-09^criticDesc|事件描述^criticRecorder1|冯亮^criticRecordDate1|2022-08-09^criticRecorder2|
	//^criticRecordDate2|1840-12-31^criticBack|亮亮^criticDeal|待处理^criticRectifAction|整改措施^criticRectifEffect|良好^RowID|1^PerDR|1
	//表扬 praiseFrom|63||1^praiseDate|2022-08-01^praiseDesc|表扬事件描述^praiseRecorder|冯亮^praiseRecordDate|2022-08-16^RowID|^PerDR|2
	function submitNewData(){
		blurAllInput()
		var parr = ''
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.getAttribute('data-id')
			domValue = (domValue == undefined?"":domValue)
			if (oneDom['type'] == 'input') {
				domValue = dom.value
			}else if (oneDom.domId != 'criticFrom') {
				domValue = dom.innerText
			}
			if (domValue == '' && oneDom['tip']!= '') {
				mui.toast(oneDom['tip'])
				return
			}
			parr += oneDom['key']+'|'+domValue+'^'
		}
		parr += 'RowID|'
		var method = 'web.INMPerSubComm:SaveCritic'
		if (pageType == '表扬备案') {
			method = 'web.INMPerSubComm:SavePraise'
			if (sourceData) {
				parr += sourceData['praiseRowID']
			}
		}else{
			if (sourceData) {
				parr += sourceData['criticRowID']
			}
		}
		plus.nativeUI.showWaiting('正在加载',{modal:false})
		parr += '^PerDR|'+perid
		var status = this.getAttribute('data-id');
		//console.log(parr)
		var pdict = {'parr':parr,'type':status}
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting()
			var msgcode =result['msgcode']
			if (msgcode != '1') {
				mui.toast(result['msg'])
				return 
			}
			
			if (unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = 'critic'
			var method = 'web.INMPersonComm:InsertPic'
			var paramDict={'id':perid,'path':path,'imgArr':unUploadFiles,'method':method,'otherParams':result['msg']+'||6||','spStr':'^'}
			if (pageType == '表扬备案') {
				paramDict['path'] = 'praise'
				paramDict['otherParams'] = result['msg']+'||5||'
			}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					plus.nativeUI.closeWaiting()
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast(errorStr)
			plus.nativeUI.closeWaiting()
		});
	}
	function getSourceList(dom){
		var typeText = dom.getAttribute('data-type')
		var pdict = {'type':typeText}
		var paramDict = {'method':'web.INMDBComm:FindPubCode','params':JSON.stringify(pdict),'type':'Query'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting()
			if (result.msgcode != '1') {
				mui.toast(typeText+':'+result.msg)
				return
			}
			var dataArr = result['data']
			for (var i = 0;i < dataArr.length;i++) {
				if (dataArr[i]['SubValue'] == dom.getAttribute('data-id')) {
					dom.innerText = dataArr[i]['SubDesc']
					break
				}
			}
		}, function(errorStr) {
			mui.toast(typeText+':'+errorStr)
			plus.nativeUI.closeWaiting()
		});
	}
	function getTransDetail(){
		var pdict = {'id':sourceData['criticRowID']};
		var method = 'web.INMPerSubComm:GetCriticItem'
		if (pageType == '表扬备案') {
			pdict = {'id':sourceData['praiseRowID']};
			method = 'web.INMPerSubComm:GetPraiseItem'
		}
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			plus.nativeUI.closeWaiting()
			if (result.msgcode != '1') {
				mui.toast(result.msg)
				return
			}
			var dataDict = result.data
			updateUI(dataDict)
		}, function(errorStr) {
		 	mui.toast(errorStr)
			plus.nativeUI.closeWaiting()
		});
	}
	
	function updateUI(dataDict){
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var domId = oneDom['domId']
			var dom = $('#'+domId)[0]
			if (oneDom['type'] == 'input') {
				dom.value= dealUndefined(dataDict[oneDom['key']])
			}else if (domId == 'criticFrom' || domId == 'criticDeal' || domId == 'criticRectifEffect'){
				var value = dealUndefined(dataDict[oneDom['key']]) 
				dom.setAttribute('data-id',value)
				if (domId == 'criticFrom') {
					getSourceList(dom)
				}else{
					dom.innerText = value
				}
			}else{
				dom.innerText = dealUndefined(dataDict[oneDom['key']])
				if (oneDom['type'] == 'list') {
					dom.setAttribute('data-id',dealUndefined(dataDict[oneDom['key']]))
					dom.innerText = dealUndefined(dataDict[oneDom['key']+'Desc'])
				}
			}
		}
		if (pageType == '表扬备案') {
			updateImageFileUI('praiseFile','fileUL',dataDict,dataDict['RowID'])
		}else{
			updateImageFileUI('criticFile','fileUL',dataDict,dataDict['RowID'])
		}
	}
	function listTypeDomTapped(){
		blurAllInput()
		if (this.id == 'criticFrom') {
			return
		}
		var textTip = this.previousElementSibling.innerText
		var tempArr
		switch (textTip){
			case '处理结果：':
				tempArr = [{value:'待处理',label:'待处理'},{value:'已处理',label:'已处理'}]
				break;
			case '整改结果：':
				tempArr = [{value:'良好',label:'良好'},{value:'一般',label:'一般'},{value:'无效',label:'无效'}]
				break;
			default:
				break;
		}
		updateOptionsUL(tempArr,$('#'+this.id)[0],'value','label')
	}
	function getDeviceDateStr(){
		var currentDate = new Date();
		var cyear = currentDate.getFullYear();
		var cmonth = currentDate.getMonth() + 1;
		var cday = currentDate.getDate();
		var thisDay = cyear + "-" 
		if (cmonth < 10) {
			thisDay +='0'
		}
		thisDay+= cmonth + "-" 
		if (cday < 10) {
			thisDay +='0'
		}
		thisDay += cday;
		return thisDay;
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (pageType == '投诉备案') {
			document.body.classList.add('Critic')
			domArr = [{'domId':'criticFrom','tip':'请选择来源','type':'list','key':'criticFrom'},
					{'domId':'criticDate','tip':'请输入发生时间','type':'date','key':'criticDate'},
					{'domId':'criticDesc','tip':'请输入事件描述','type':'input','key':'criticDesc'},
					{'domId':'criticRecorder1','tip':'','key':'criticRecorder1'},
					{'domId':'criticRecordDate1','tip':'','key':'criticRecordDate1'},
					{'domId':'criticRecorder2','tip':'','key':'criticRecorder2'},
					{'domId':'criticRecordDate2','tip':'','key':'criticRecordDate2'},
					{'domId':'criticBack','tip':'请输入反馈人','type':'input','key':'criticBack'},
					{'domId':'criticDeal','tip':'','type':'list','key':'criticDeal'},
					{'domId':'criticRectifAction','tip':'','type':'input','key':'criticRectifAction'},
					{'domId':'criticRectifEffect','tip':'','type':'list','key':'criticRectifEffect'}]
		}else{
			document.body.classList.add('Praise')
			//praiseFrom|63||1^praiseDate|2022-08-01^praiseDesc|表扬事件描述^praiseRecorder|冯亮^praiseRecordDate|2022-08-16^RowID|^PerDR|2
			domArr = [{'domId':'praiseFrom','tip':'请选择表扬来源','type':'list','key':'praiseFrom'},
					{'domId':'criticDate','tip':'请输入发生时间','type':'date','key':'praiseDate'},
					{'domId':'criticDesc','tip':'','type':'input','key':'praiseDesc'},
					{'domId':'criticRecorder1','tip':'','key':'praiseRecorder'},
					{'domId':'criticRecordDate1','tip':'','key':'praiseRecordDate'}]
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		perid = perInfo['loginPerID']
		if (sourceData) { //修改
			plus.nativeUI.showWaiting('正在加载',{modal:false})
			getTransDetail()
		}else{
			$('#criticRecorder1')[0].innerText = perInfo['loginName']
			$('#criticRecordDate1')[0].innerText = getDeviceDateStr()
		}
		if (pageType == '投诉备案') {
			$('#criticRecorder2')[0].innerText = perInfo['loginName']
			$('#criticRecordDate2')[0].innerText = getDeviceDateStr()
		}
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']

		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		if (pageType == '投诉备案') {
			mui('#UL').on('tap','.listType',listTypeDomTapped);
		}
		
		mui('#UL').on('tap','#criticFrom',downListElementTapped);
		mui('#UL').on('tap','#praiseFrom',downListElementTapped);
		window.addEventListener('resize',windowResize)
		$('#addFileBtn').on('change','input',function(){
			blurAllInput()
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles = afterSelectImageFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-icon-closeempty',function(){
			var method = 'web.INMPersonComm:DeleteCert'
			var otherParams = '||6||'
			if (pageType == '表扬备案') {
				otherParams = '||5||'
			}
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid,'otherParams':otherParams}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	
})(mui)

// 
// parr: 3||6||Fileupdate/images/critic/511/Screenshot-2022-09-26-10-09-25-583-HBuilder.png
// Fileupdate/images/critic/511/Screenshot-2022-09-26-10-09-25-583-HBuilder.png

